/**
 * Copyright (c) 2004-2015 All Rights Reserved.
 */
package com.zbest.component.session;

import java.io.Serializable;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * redis作为存储的session实现
 *
 * @author czhou3306@gmail.com
 * @version $Id: RedisSession.java, v0.1 2015年6月9日 上午10:41:30 czhou3306@gmail.com Exp $
 */
public class RedisSession implements QianSession, Serializable {

    /**  */
    private static final long   serialVersionUID = -535233615598371364L;

    /**数据集合*/
    private Map<String, Object> data             = new ConcurrentHashMap<String, Object>();

    /**会话id*/
    private String              sessionId;

    /**创建时间*/
    private long                gmtCreate;

    /**最后一次进入时间*/
    private long                gmtLastAccess;

    /**过期时间 ，以s为单位*/
    private int                 expireTime;

    /**
     * Setter method for property <tt>sessionId</tt>.
     * 
     * @param sessionId value to be assigned to property sessionId
     */
    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    /**
     * Getter method for property <tt>expireTime</tt>.
     * 
     * @return property value of expireTime
     */
    public int getExpireTime() {
        return expireTime;
    }

    /**
     * Setter method for property <tt>expireTime</tt>.
     * 
     * @param expireTime value to be assigned to property expireTime
     */
    public void setExpireTime(int expireTime) {
        this.expireTime = expireTime;
    }

    /** 
     * @see com.zbest.component.session.QianSession#getSessionId()
     */
    @Override
    public String getSessionId() {
        return sessionId;
    }

    /** 
     * @see com.zbest.component.session.QianSession#getAttribute(java.lang.String)
     */
    @Override
    public Object getAttribute(String name) {
        return data.get(name);
    }

    /** 
     * @see com.zbest.component.session.QianSession#getValue(java.lang.String)
     */
    @Override
    public Object getValue(String name) {
        return data.get(name);
    }

    /** 
     * @see com.zbest.component.session.QianSession#getAttributeNames()
     */
    @Override
    public Enumeration<String> getAttributeNames() {
        return Collections.enumeration(data.keySet());
    }

    /** 
     * @see com.zbest.component.session.QianSession#getValueNames()
     */
    @Override
    public String[] getValueNames() {
        Set<String> attrs = data.keySet();
        return attrs.toArray(new String[0]);
    }

    /** 
     * @see com.zbest.component.session.QianSession#setAttribute(java.lang.String, java.lang.Object)
     */
    @Override
    public void setAttribute(String name, Object value) {
        data.put(name, value);
    }

    /** 
     * @see com.zbest.component.session.QianSession#putValue(java.lang.String, java.lang.Object)
     */
    @Override
    public void putValue(String name, Object value) {
        data.put(name, value);
    }

    /** 
     * @see com.zbest.component.session.QianSession#removeAttribute(java.lang.String)
     */
    @Override
    public void removeAttribute(String name) {
        data.remove(name);
    }

    /** 
     * @see com.zbest.component.session.QianSession#setMaxInactiveInterval(int)
     */
    @Override
    public void setMaxInactiveInterval(int interval) {
        this.expireTime = interval;
    }

    /** 
     * @see com.zbest.component.session.QianSession#getMaxInactiveInterval()
     */
    @Override
    public int getMaxInactiveInterval() {

        return this.expireTime;
    }

    /** 
     * @see com.zbest.component.session.QianSession#getGmtCreate()
     */
    @Override
    public long getGmtCreate() {
        return gmtCreate;
    }

    /** 
     * @see com.zbest.component.session.QianSession#getGmtLastAccess()
     */
    @Override
    public long getGmtLastAccess() {
        return gmtLastAccess;
    }

    /**
     * Setter method for property <tt>gmtCreate</tt>.
     * 
     * @param gmtCreate value to be assigned to property gmtCreate
     */
    public void setGmtCreate(long gmtCreate) {
        this.gmtCreate = gmtCreate;
    }

    /**
     * Setter method for property <tt>gmtLastAccess</tt>.
     * 
     * @param gmtLastAccess value to be assigned to property gmtLastAccess
     */
    public void setGmtLastAccess(long gmtLastAccess) {
        this.gmtLastAccess = gmtLastAccess;
    }

    /**
     * Getter method for property <tt>data</tt>.
     * 
     * @return property value of data
     */
    public Map<String, Object> getData() {
        return data;
    }

    /**
     * Setter method for property <tt>data</tt>.
     * 
     * @param data value to be assigned to property data
     */
    public void setData(Map<String, Object> data) {
        this.data = data;
    }

}
